package com.jinhua.admin.dao.impl;
import com.jinhua.mybatis.util.MybatisPlus;
import com.jinhua.mybatis.util.MybatisPlusBuild;
import com.jinhua.config.IdUtil;
import com.jinhua.admin.dao.ProductDao;
import com.jinhua.admin.entity.ProductPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
* 商品表的dao层
*
*/

@Repository
public class ProductDaoImpl implements ProductDao {
        private final int PAGE_SIZE = 10;
    @Override
    public int saveProductIfNotExist(ProductPo productPo) {
        return productPo.save();
    }
    @Override
    public int modProduct(ProductPo productPo) {
        return productPo.update(productPo.build().eq(ProductPo::getId, productPo.getId()));
    }
    @Override
    public int delProductByIds(Long... ids) {
        ProductPo productPo = new ProductPo();
        productPo.setDeletedFlag(Boolean.TRUE);
        return productPo.update(productPo.build().in(ProductPo::getId, ids));
    }
    @Override
    public ProductPo findProductById(Long id) {
        MybatisPlusBuild build = new MybatisPlusBuild(ProductPo.class);
        build.eq(ProductPo::getDeletedFlag, Boolean.FALSE);
        build.eq(ProductPo::getId, id);
        return MybatisPlus.getInstance().findOne( new ProductPo(), build);
    }
    @Override
    public List<ProductPo> findProductByAll(ProductPo productPo, Integer page, Integer size) {
        MybatisPlusBuild build = new MybatisPlusBuild(ProductPo.class);
        build.eq(ProductPo::getDeletedFlag, Boolean.FALSE);
        if (productPo.getId() == null) {
            if (StringUtils.isNotEmpty(productPo.getName())) {
                build.like(ProductPo::getName,productPo.getName());
            }
            if (productPo.getProductCount()!=null ) {
                build.eq(ProductPo::getProductCount,productPo.getProductCount());
            }
            if (page == null) {
                page = 1;
            }
            IdUtil.setTotal(build);
            build.page(page, size==null?PAGE_SIZE:size);
        }
        else {
            build.eq(ProductPo::getId, productPo.getId());
        }
        return MybatisPlus.getInstance().findAll( new ProductPo()    , build);
    }
}
